// pages/search/search.js
import {getDefaultKeyword,getHotList,getSearchSuggest} from '~/service/search'

let timer = null; // 节流控制
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showKeyword:'', //默认关键字
    hotList:[], //热搜列表
    keywords:'', //搜索关键字
    searchSuggestList:[], //搜索建议列表
    historyList:[],
    realkeyword:''
    
  },

  // 请求默认搜索关键字
  async getDefaultKeywordData(){
    let {data} = await getDefaultKeyword()
    console.log(data)
    let showKeyword = data.showKeyword
    let realkeyword = data.realkeyword
    this.setData({
      showKeyword,
      realkeyword
    })
  },
  // 请求热搜列表
  async getHotListData(){
    let {data} = await getHotList()
    this.setData({
      hotList:data
    })
  },
  // 搜索栏发生变化回调
   onSearchInput(e){
    let keywords = e.detail.value.trim();
    this.setData({
      keywords
    })
    if(timer) return
    timer = setTimeout(()=>{
        this.getSearchSuggestData();
        timer = null;
      },500)
  },
  async getSearchSuggestData(){
    // let {result:{allMatch}} = await getSearchSuggest({keywords:this.data.keywords,type:'mobile'});
    let {result:{songs}} = await getSearchSuggest({keywords:this.data.keywords});
    this.setData({
      searchSuggestList:songs,
    })
  },
  // 清空搜索栏
  clearSearch(){
    console.log(1);
    this.setData({
      keywords:''
    })
  },
  // 搜索歌曲(点击按钮)
  onSearch(){
    console.log(this.data.realkeyword)
    console.log(this.data.showKeyword)
    if(this.data.keywords==''){
      this.setData({
        keywords:this.data.realkeyword
      })
    }
    let keyword = this.data.keywords;
    let historyArr = this.data.historyList;
    if(!historyArr.includes(keyword)){
      historyArr.push(keyword)
    }
    this.setData({
      historyList:historyArr
    })
    wx.setStorageSync('searchHistory', historyArr)
    this.getSearchSuggestData();
  },
  // 删除搜索历史
  deleteSearchHistory(){
    wx.showModal({
      title: '确定要删除历史记录吗？',
      // content: '',
      complete: (res) => {
        if (res.confirm) {
          wx.removeStorageSync('searchHistory')
          this.setData({
            historyList:[]
          })
        }
      }
    })
  },
    // 点击歌曲跳转到歌曲详情页面(播放)
    toSongDetail(e){
      let {songid,index,name} = e.currentTarget.dataset;
      let historyArr = this.data.historyList;
    if(!historyArr.includes(name)){
      historyArr.push(name)
    }
    this.setData({
      historyList:historyArr
    })
    wx.setStorageSync('searchHistory', historyArr)
      this.setData({
        tapIndex:index
      })
      wx.navigateTo({
        url: `/pages/songDetail/songDetail?songid=${songid}`,
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let searchHistoryArr = wx.getStorageSync('searchHistory');
    if(searchHistoryArr){
      let historyList = this.data.historyList;
      let concatArr = historyList.concat(searchHistoryArr)
      this.setData({
        historyList:concatArr
      })
    }

    this.getDefaultKeywordData()
    this.getHotListData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})